# coding=utf-8

# @Time : 2019/12/15 15:14
# @Author : zhusc
# @File : logic/cus_logic.py
# @Desc : 客户数据相关处理

class CustomerLogic:

    def get_customer_buy_info(self, mysql_db, cus_id, ts_start, ts_end):
        """ 获取客户的购买信息(下单)
        """
        sql_sale_info = " \
            SELECT \
                1 type, \
                og.order_id, \
                og.goods_id, \
                og.goods_num, \
                og.goods_num * og.goods_price total, \
                oas.id after_sale_id, \
                IFNULL(oasg.refund_num, 0) refund_num, \
                IFNULL( \
                    oasg.refund_num * og.goods_price, \
                    0 \
                ) refund_order_amount \
            FROM \
                `order` o \
            LEFT JOIN `order_goods` og ON o.id = og.order_id \
            LEFT JOIN `order_after_sale` oas ON og.order_id = oas.order_id \
            LEFT JOIN `order_after_sale_goods` oasg ON oas.id = oasg.order_after_sale_id \
            WHERE \
                o.user_id = %d \
            AND o.verify_at >= '%s' \
            AND o.verify_at <= '%s' \
            AND og.shipping_status = 2 \
        " % (customer_id, ts_s, ts_e)
        sale_res = mysql_db.select(sql_sale_info)
        return sale_res

    def get_customer_inlay_info(self, mysql_db, cus_id, ts_start, ts_end):
        """ 获取客定镶嵌每日下单情况，包含成交订单数，售出金额
        """
        sql_sale_info = "\
            SELECT \
                2 type, \
                io.id, \
                iog.goods_id, \
                iog.inlay_num, \
                iog.inlay_num * g.business_price total, \
                IFNULL(oasg.refund_num, 0) refund_num, \
                IFNULL( \
                    oasg.refund_num * g.business_price, \
                    0 \
                ) refund_order_amount \
            FROM \
                `inlay_order` io \
            LEFT JOIN `order` o ON io.order_id = o.id \
            LEFT JOIN `inlay_order_goods` iog ON io.id = iog.inlay_order_id \
            LEFT JOIN bojem_microservice_goods.goods g ON iog.goods_id = g.id \
            LEFT JOIN `order_after_sale` oas ON io.order_id = oas.order_id \
            LEFT JOIN `order_after_sale_goods` oasg ON oas.id = oasg.order_after_sale_id \
            LEFT JOIN `inlay_order_pay_info` iopi ON io.id = iopi.inlay_order_id \
            WHERE \
                o.user_id = %d \
            AND io.inlay_status = 7 \
            AND iog.delivery_id <> 0 \
            AND iopi.verify_res = 1 \
            AND iopi.verify_at >= '%s' \
            AND iopi.verify_at <= '%s' \
        " % (customer_id, ts_s, ts_e)
        sale_res = mysql_db.select(sql_sale_info)
        return sale_res

    def get_customer_refund_info(self, mysql_db, cus_id, ts_start, ts_end):
        """ 获取客户的购买信息(退款)
        """
        refund_res = {}
        return refund_res

    def cal_sale_num_amount(self, customer_sale_info):
        """ 根据查询结果计算商品当日的订单数量和售出金额
        """
        sale_info_return = {
            'order_num': 0, 
            'order_amount': 0.00,
            'inlay_num':0,
            'inlay_amount':0.00,
            'refund_order_num':0,
            'refund_order_amount':0.00
        }

        for info in goods_sale_info:
            if info['refund_num'] != 0 :
                sale_info_return['refund_order_num'] += 1
                sale_info_return['refund_order_amount'] += info['refund_order_amount']
            else :
                if info['type'] == 1 :
                    sale_info_return['order_num'] += 1
                    sale_info_return['order_amount'] += info['total']
                else :
                    sale_info_return['inlay_num'] += 1
                    sale_info_return['inlay_amount'] += info['inlay_amount']
        return sale_info_return

    def cal_refund_num_amount(self, customer_refund_info):
        """ 根据查询结果计算当日的退款数量和退款金额
        """
        refund_info_return = {'refund_num': 0, 'refund_amount': 0.00}
        for info in customer_refund_info:
            refund_info_return['sale_num'] += info['num']
            refund_info_return['sale_amount'] += info['totle']
        return refund_info_return
